<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GlossAI</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        /* 侧边栏隐藏/显示效果 */
        #sidebar {
            width: 250px;
            height: 100%;
            position: fixed;
            top: 0;
            left: -250px;
            background-color: #f8f9fa;
            transition: left 0.3s ease;
        }

        #sidebar.active {
            left: 0;
        }

        /* 美化菜单按钮 */
        #toggleButton {
            position: fixed;
            bottom: 20px;
            left: 20px;
            background-color: #4F46E5; /* Indigo */
            color: white;
            padding: 10px;
            border-radius: 50%;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease;
        }

        #toggleButton:hover {
            background-color: #3730A3; /* Darker Indigo */
        }
    </style>
</head>
<body class="bg-gray-100 h-screen">

<!-- Container -->
<div class="flex h-full">
    <!-- 侧边栏 -->
    <div id="sidebar" class="shadow-md p-4">
        <h2 class="text-2xl font-bold mb-4">选择菜单</h2>
        <ul>
            <li class="mb-4"><a href="#" class="text-blue-500">抽取术语</a></li>
            <li class="mb-4"><a href="#" class="text-blue-500">广场</a></li>
            <li class="mb-4"><a href="#" class="text-blue-500">个人主页</a></li>
        </ul>
    </div>

    <!-- 美化后的菜单按钮 -->
    <button id="toggleButton">
        <!-- 使用图标替换文字 -->
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"/>
        </svg>
    </button>

    <!-- Left Panel -->
    <div class="w-1/3 bg-gray-50 p-8 shadow-md">
        <h1 class="text-4xl font-bold text-gray-800 mb-8">GlossAI</h1>

        <textarea id="inputText" rows="6" cols="60" placeholder="Enter your text here..."
                  class="w-full p-4 border border-gray-300 rounded mb-4 focus:outline-none focus:ring-2 focus:ring-blue-500"></textarea>

        <div class="mb-4">
            <label for="queryType" class="block text-gray-700 mb-2">Choose query type:</label>
            <select id="queryType"
                    class="w-full border p-2 rounded focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="model">大模型查询</option>
                <option value="database">术语库查询</option>
            </select>
        </div>

        <div class="mb-4">
            <label for="modelChoice" class="block text-gray-700 mb-2">Choose a model:</label>
            <select id="modelChoice"
                    class="w-full border p-2 rounded focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="glm-4-flash">智谱清言 GLM-4-flash</option>
                <option value="GPT-3.5">OpenAI GPT-3.5</option>
                <option value="Spark-Max">科大讯飞 Spark-Max</option>
            </select>
        </div>

        <button onclick="extractTerms()"
                class="w-full bg-green-500 text-white p-3 rounded mt-4 hover:bg-green-700 transition duration-200">
            Extract Terminology
        </button>
    </div>

    <!-- Right Panel (Output Section) -->
    <div class="w-2/3 p-8 overflow-y-auto">
        <div id="output" class="text-left">
            <!-- Extraction results will be displayed here -->
        </div>
    </div>
</div>


<script>
    // 菜单按钮点击事件
    const toggleButton = document.getElementById("toggleButton");
    const sidebar = document.getElementById("sidebar");

    toggleButton.addEventListener("click", function () {
        sidebar.classList.toggle("active");
    });

    async function extractTerms() {
        const inputText = document.getElementById("inputText").value;
        const modelChoice = document.getElementById("modelChoice").value;
        const queryType = document.getElementById("queryType").value;
        const outputDiv = document.getElementById("output");

        if (inputText.trim() === "") {
            outputDiv.innerHTML = "<p class='text-red-500'>抽取栏为空，请输入文本！</p>";
            return;
        }

        outputDiv.innerHTML = "<p class='text-blue-500'>正在查询，请稍候...</p>";

        try {
            let apiUrl = queryType === "model" ? "http://127.0.0.1:5000/extract-terms" : "http://127.0.0.1:5000/search_terms";
            let requestBody = {text: inputText, model: modelChoice};

            const response = await fetch(apiUrl, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(requestBody)
            });

            const result = await response.json();
            outputDiv.innerHTML = "";

            if (queryType === "database" && Object.keys(result).length === 0) {
                outputDiv.innerHTML = "<p class='text-red-500'>没有找到与术语库匹配的术语</p>";
                return;
            }

            for (const key in result) {
                if (result.hasOwnProperty(key)) {
                    const term = result[key];
                    outputDiv.innerHTML += `
                            <div class="bg-white shadow-md rounded p-4 mb-4">
                                <p><strong>Chinese:</strong> ${term.chinese}</p>
                                <p><strong>English:</strong> ${term.english}</p>
                                <p><strong>Example:</strong> ${term.example}</p>
                            </div>
                        `;
                }
            }

            outputDiv.innerHTML += "<p class='text-green-500'>查询成功！</p>";
        } catch (error) {
            outputDiv.innerHTML = `<p class='text-red-500'>查询过程中出现错误，请重试！</p>
                                        <p>错误信息: ${error.message}</p>`;
        }
    }
</script>

</body>
</html>
